package com.stn.zjx.givingtreasure.activity.set;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jungly.gridpasswordview.GridPasswordView;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.base.BaseActivity;
import com.stn.zjx.givingtreasure.domain.StatusDomain;
import com.stn.zjx.givingtreasure.event.NetWorkEvent;
import com.stn.zjx.givingtreasure.net.BaseApi;
import com.stn.zjx.givingtreasure.net.impl.PayNetImpl;
import com.stn.zjx.givingtreasure.net.impl.UserNetImpl;
import com.stn.zjx.givingtreasure.service.UserService;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PayPasswordSetActivity extends BaseActivity {

    @BindView(R.id.back_bt)
    RelativeLayout backBt;
    @BindView(R.id.action_title)
    TextView actionTitle;
    @BindView(R.id.web_title)
    TextView webTitle;
    @BindView(R.id.action_add_bt)
    RelativeLayout actionAddBt;
    @BindView(R.id.action_refresh)
    RelativeLayout actionRefresh;
    @BindView(R.id.exit)
    RelativeLayout exit;
    @BindView(R.id.sure_bt)
    TextView sureBt;
    @BindView(R.id.grid_password_edit)
    GridPasswordView gridPasswordEdit;
    @BindView(R.id.go_sure)
    Button goSure;

    @Override
    public void networkStatus(NetWorkEvent netWorkEvent) {

    }

    @Override
    public void initComponent() {
        actionTitle.setText(getString(R.string.modify_pay_password));
        gridPasswordEdit.setOnPasswordChangedListener(new GridPasswordView.OnPasswordChangedListener() {
            @Override
            public void onTextChanged(String psw) {
                if (psw.length() >= 6) {
                    goSure.setClickable(true);
                    goSure.setBackgroundResource(R.drawable.shape_coloraccent_5dp);
                } else {
                    goSure.setClickable(false);
                    goSure.setBackgroundResource(R.drawable.shape_darkgray_5dp);
                }
            }

            @Override
            public void onInputFinish(String psw) {

            }
        });
        goSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                  if(gridPasswordEdit.getPassWord().trim().length()!=6){
                        showToast(getString(R.string.pay_password_tip));
                      return;
                  }
                reset_paypassword(gridPasswordEdit.getPassWord());
            }
        });
        goSure.setClickable(false);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_set_paypassword);
        ButterKnife.bind(this);
        initComponent();
    }

    @OnClick({R.id.back_bt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_bt:
                finish();
                break;

        }
    }

    public  void reset_paypassword(String userPrivatepwd){
         if(UserService.getInstance(this).getOnlineLoginUser()==null){
             return;
         }
        PayNetImpl
                .reset_paypassword(UserService.getInstance(this).getOnlineLoginUser().getToken(),
                        userPrivatepwd, "reset_paypassword",
                        new StringCallback() {
                            @Override
                            public void onStart(Request<String, ? extends Request> request) {
                                super.onStart(request);
                                showLoading();
                            }

                            @Override
                            public void onFinish() {
                                super.onFinish();
                                  dissmissLoading();
                            }

                            @Override
                            public void onSuccess(Response<String> response) {
                                StatusDomain domain=new Gson().fromJson(response.body(),StatusDomain.class);
                                switch (domain.getStatus()){
                                    case BaseApi.REQUEST_SUCCESS_CODE:
                                        showToast(getString(R.string.reset_pay_password_success));
                                        finish();
                                        break;
                                    case BaseApi.REQUEST_ERROR_CODE:
                                        showToast(getString(R.string.reset_pay_password_error));
                                        break;
                                     default:
                                         showToast(domain.getMessage());
                                         break;
                                }
                            }
                        });
    }
}
